﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;
using log4net;

namespace Meqs.Logic.Implement
{
    public class StuCauseEvalHistoryLogic : CommonLogic<StuCauseEvalHistory, int>, IStuCauseEvalHistoryLogic
    {
        #region Implementation of ICommonLogic<StuCauseEvalHistory, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(StuCauseEvalHistory entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(StuCauseEvalHistory entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public StuCauseEvalHistory FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalHistory> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuCauseEvalHistory> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion


        public int FindTeacherCount(Hashtable ht)
        {
            try
            {
                string statementName = "StuCauseEvalHistory.FindTeacherCount";
                return ExecuteQueryForObject<int>(statementName, ht);
            }
            catch (Exception ex)
            {
                //_log.Error("查询对象数量出错：" + ex.Message, ex);
                throw;
            }
        }

        /// <summary>
        /// 查询带有课程名的总学分评估记录
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <returns></returns>
        public IList<VStuCauseEvalHistory> FindByCauseName(string schoolMark, int schoolYear, int term)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("schoolYear", schoolYear);
            table.Add("term", term);

            return FindCustomer<VStuCauseEvalHistory>("StuCauseEvalHistory.FindWithCauseName", table);
        }


        /// <summary>
        /// 获取学分评估的类型以及评估项信息
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="causeId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IList<VStuCauseEvalHistoryTypeItem> FindWithTypeItme(string schoolMark, int schoolYear, int term, int historyId, int causeId, int typeId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("schoolYear", schoolYear);
            table.Add("term", term);
            table.Add("historyId", historyId);
            table.Add("causeId", causeId);
            table.Add("typeId", typeId);

            return FindCustomer<VStuCauseEvalHistoryTypeItem>("StuCauseEvalHistory.FindWithTypeItme", table);
        }

        /// <summary>
        /// 获取当前评价状态
        /// </summary>
        public IList<StuCauseEvalState> GetState(int schoolYear, int term, String schoolMark, IList<int> clsList, int causeID)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("Year", schoolYear);
            ht.Add("Term", term);
            if(clsList != null && clsList.Count > 0)
                ht.Add("IDList", clsList);
            ht.Add("CauseID", causeID);
            IList<StuCauseEvalState> result = this.ExecuteQueryForList<StuCauseEvalState>("StuCauseEvalHistory.GetState", ht);
            return result;
        }

        public IList<StuCauseEvalState> GetState(int schoolYear, int term, String schoolMark, IList<int> clsList)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("Year", schoolYear);
            ht.Add("Term", term);
            if (clsList != null && clsList.Count > 0)
                ht.Add("IDList", clsList);
            IList<StuCauseEvalState> result = this.ExecuteQueryForList<StuCauseEvalState>("StuCauseEvalHistory.GetStateEx", ht);
            return result;
        }

        public IList<StuCauseEvalState> GetStateWithDate(int schoolYear, int term, String schoolMark, IList<int> clsList, String date)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("Year", schoolYear);
            ht.Add("Term", term);
            if (clsList != null && clsList.Count > 0)
                ht.Add("IDList", clsList);
            ht.Add("Date", Convert.ToDateTime(date));
            IList<StuCauseEvalState> result = this.ExecuteQueryForList<StuCauseEvalState>("StuCauseEvalHistory.GetStateWithDate", ht);
            return result;
        }

        /// <summary>
        /// 获取评价历史记录
        /// </summary>
        /// <param name="schoolYear"></param>
        /// <param name="term"></param>
        /// <param name="causeID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public StuCauseEvalHistory GetHistoryRecordByTerm(int schoolYear, int term,int causeID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("Year", schoolYear);
            ht.Add("Term", term);
            ht.Add("CauseID", causeID);
            ht.Add("SchoolMark", schoolMark);

            return FindCustomerByID<StuCauseEvalHistory>("StuCauseEvalHistory.GetHistoryRecordByTerm", ht);
        }


        public int UpdateStatus(string schoolMark, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("schoolYear", schoolYear);
            ht.Add("term", term);
            ht.Add("SchoolMark", schoolMark);
            ht.Add("status", 1);

            return UpdateCustomer("StuCauseEvalHistory.UpdateStatus", ht);
        }


        public int UpdateStatus(string schoolMark,string ids, int schoolYear, int term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("schoolYear", schoolYear);
            ht.Add("term", term);
            ht.Add("SchoolMark", schoolMark);
            ht.Add("status", 1);
            ht.Add("ids", ids);

            return UpdateCustomer("StuCauseEvalHistory.UpdateStatus", ht);
        }

        public int UpdateStatus(string schoolMark, int historyId, int schoolYear, int term, int status)
        { 
            Hashtable ht = new Hashtable();
            ht.Add("schoolYear", schoolYear);
            ht.Add("term", term);
            ht.Add("SchoolMark", schoolMark);
            ht.Add("status", status);
            ht.Add("RID", historyId);

            return UpdateCustomer("StuCauseEvalHistory.UpdateStatus", ht);
        }


        public int InsertNew(string schoolMark, int schoolYear, int term,int causeId,int userId)
        {
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolMark);
            table.Add("SchoolYear", schoolYear);
            table.Add("Term", term);
            table.Add("CauseID", causeId);
            table.Add("Status", 0);
            table.Add("ManagerID", userId);

            return (int)InsertCustomer("StuCauseEvalHistory.InsertNew", table);
        }

        //[Transaction(TransactionMode.Requires)]
        //public void CreateNewTermHistory(string schoolMark, int schoolYear, int term, int causeId, int userId)
        //{
        //    //先添加history

        //    int historyId = InsertNew(schoolMark, schoolYear, term, causeId, userId);

        //    //查找上学期规则
        //    if (historyId > 0)
        //    {
                 
        //    }

        //    //插入旧规则

        //}
    }
}